import express from 'express';
import { aiQuestionGenerationController } from '../controllers/aiQuestionGenerationController';
import { roleAuth } from '../middleware/roleAuth';
import { Role } from '@prisma/client';
import { validateRequest } from '../middleware/validation';
import { z } from 'zod';

const router = express.Router();

// 验证Schema
const generationCriteriaSchema = z.object({
  body: z.object({
    subject: z.string().min(1, '学科不能为空'),
    topic: z.string().min(1, '主题不能为空'),
    difficulty: z.enum(['EASY', 'MEDIUM', 'HARD']),
    type: z.enum(['MULTIPLE_CHOICE', 'OPEN_ENDED']),
    count: z.number().int().min(1).max(50),
    keywords: z.array(z.string()).optional(),
    excludeIds: z.array(z.string().uuid()).optional()
  })
});

const similarQuestionsSchema = z.object({
  params: z.object({
    questionId: z.string().uuid('无效的题目ID')
  }),
  body: z.object({
    count: z.number().int().min(1).max(10)
  })
});

const knowledgePointsSchema = z.object({
  body: z.object({
    knowledgePoints: z.array(z.string()).min(1, '至少需要一个知识点'),
    subject: z.string().min(1, '学科不能为空'),
    difficulty: z.enum(['EASY', 'MEDIUM', 'HARD']),
    type: z.enum(['MULTIPLE_CHOICE', 'OPEN_ENDED']),
    count: z.number().int().min(1).max(50)
  })
});

// 路由配置
router.post(
  '/generate',
  roleAuth([Role.ADMIN, Role.TEACHER]),
  validateRequest(generationCriteriaSchema),
  aiQuestionGenerationController.generateQuestions
);

router.post(
  '/:questionId/similar',
  roleAuth([Role.ADMIN, Role.TEACHER]),
  validateRequest(similarQuestionsSchema),
  aiQuestionGenerationController.generateSimilarQuestions
);

router.post(
  '/generate-by-knowledge-points',
  roleAuth([Role.ADMIN, Role.TEACHER]),
  validateRequest(knowledgePointsSchema),
  aiQuestionGenerationController.generateByKnowledgePoints
);

export default router;
